<!--  Displays the sport matches and also placing bets javascript mechanism -->

<script language="javascript">
function getPosition(obj){
    return [obj.getAbsoluteLeft(),obj.getAbsoluteTop()];    
}


  var selectedTeams = {};
  function selectTeam(dom,matchID,teamID) {
  	console.log("selectedTeam");
  	who = getPosition(dom);
    //shiftSideBar(document.getElementById('sideBar'),who[1])

    var index = "index" + matchID;
    var originalSelected = selectedTeams[index];
    var inputID = "bet_match_id_" + matchID + "_team_id";
    if(originalSelected) {
      originalDOM = originalSelected.dom;
      originalTeamID  = originalSelected.team_id;
      originalDOM.removeClassName("team-selected");
      document.getElementById(inputID).setValue(''); //clear 
      selectedTeams[index] = null;
      if(originalTeamID == teamID) {
        return;
      }
    }
    selectedTeams[index] = {input:document.getElementById(inputID),dom: dom,team_id: teamID}
    //add css class name
    dom.addClassName("team-selected");
    $(inputID).setValue(teamID);
    flashDiv(document.getElementById('betSide'),"#f0f0f0")
  }

  function resetBetsForm(){
    for(k in selectedTeams) {
      var team = selectedTeams[k];
      if(team) {
        team.dom.removeClassName("team-selected");
        team.input.setValue('');
        selectedTeams[k] = null;
      }
    }
    $("bets-form").reset();
  }
</script>
<% if @matches.blank? %>
  <div class="empty">Come back later for new matches!</div>
<% else %>
  <%form_tag bets_path(:sport_id=>@sport),:id=>"bets-form" do %>
    <%= hidden_field_tag 'referer',url_for(params) %>
    <% @matches.group_by{|match| match.begin_at.to_date }.each do |time,matches| %>
      <%= render :partial=>"matches/matches",:locals=>{:matches=>matches,:time=>time} %>
    <% end %>
    <br />
    <%= will_paginate @matches if @matches.respond_to?(:total_pages) %>
  <%end %>
<% end %>


<% content_for :side do %>
<div id="sideBar" style="position: relative;">

	<div id="betSide">
	  <br/>
	  <%= link_to_function "Place Bet","$('bets-form').submit()",:class=>"place-bets",:onmouseover=>"this.addClassName('place-bets-hover');",:onmouseout=>"this.removeClassName('place-bets-hover');"%>
	  <br/>
	</div>
	<%= link_to_function "Reset","resetBetsForm()",:class=>"reset-bets",:onmouseover=>"this.addClassName('reset-bets-hover');",:onmouseout=>"this.removeClassName('reset-bets-hover');"%>
	<%= render :partial=>"sport_side",:locals=>{:sport=>@sport,:sport_side=>@sport} %>

</div>

<% end %>

